import React from 'react';
import {inject, observer} from 'mobx-react';
import {toJS} from 'mobx';
import {Spin, Modal, Row, Col} from 'antd';
import {WeaNewScrollPagination, WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import WeesoTop from './childComponents/WeesoTop';
import WeesoInput from './childComponents/WeesoInput';
import CommonContent from './childComponents/CommonContent';
import SearchTools from './childComponents/SearchTools';
import ImagesContent from './childComponents/ImagesContent';
import Adlink from './childComponents/Adlink';

const store = ['routing', 'weesoStore', 'weesoSeachSetStore']
@inject(...store)
@observer
export default class WeesoResult extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
    }
  }

  viewSubCompany = (subcompanyid) => {
    window.open(`/spa/hrm/engine.html#/hrmengine/organization?showTree=false&isView=1&type=subcompany&id=${subcompanyid}`);
  }

  viewDepartment = (departmentid) => {
    window.open(`/spa/hrm/engine.html#/hrmengine/organization?showTree=false&isView=1&type=department&id=${departmentid}`);
  }
  
  
  componentDidMount() {
    const {weesoStore, routing} = this.props;
    const { getAdConditions, recordType } = this.props.weesoSeachSetStore;
    //处理外部跳转进入微搜结果页面
    //value: 输入框的值 type:搜索类型  searchType: 内容/图片  msg: 服务未启动的报错信息
    const {location} = this.props;
    const {query = {value: '', type: '', searchType: '', msg}} = location;
    const { search } = this.props.location;
    weesoStore.setState(
        { urlParams: query || {} },
        ()=>{
            if(query.value !== undefined){//需要!== undefined作为判断，不能去掉
                weesoStore.isCustomer = true;
                if (query.value == '' && query.type == 'ALL') {
                    routing.push(`/main/esearch/weeso${search}`);
                }else if(query.searchType == 'ERROR'){
                    Modal.error({
                        title: getLabel(15172, "系统提示"),
                        content: query.msg,
                    });
                }else {
                    weesoStore.setState(
                        {
                            inputValue: query.value || '',
                            defalutMoreKey: query.type || 'ALL',
                            searchType: query.searchType || 'CONTENT'
                        }
                    );
                    const callRouting = () => routing.push(`/main/esearch/weeso${search}`);
                    const callSearch = () => weesoStore.inputValue !== '' && weesoStore.doSearch();
                    weesoStore.getBaseData(false, callRouting, callSearch);
                }
            }
            weesoStore.showSubmit();
            weesoStore.initWeesoResult();
            weesoStore.changeKey(query.type || 'ALL', false);
            weesoStore.isCustomer = false;
            weesoStore.showAdmin = false;
            if(recordType !== 'commonContion') {
              getAdConditions();
            }
        });

  }
  
  componentWillReceiveProps(nextProps) {

    //处理外部跳转进入微搜结果页面
    const {location, weesoStore, routing} = this.props;
    
    const {query = {value: '', type: '', searchType: '', mag: ''}} = location;
    const
      nextVal = nextProps.location.query.value,
      nextType = nextProps.location.query.type,
      nextSearchType = nextProps.location.query.searchType,
      nextMsg = nextProps.location.query.msg;
    weesoStore.setState(
        { urlParams: nextProps.location.query || {} },
        ()=>{
            const { search } = this.props.location;
            if (this.props.location.key !== nextProps.location.key && weesoStore && nextVal && nextType) {
                //if (weesoStore && (query.value || nextVal) && (nextType !== query.type || nextVal !== query.value)) {
                weesoStore.isCustomer = true;
                if (nextVal == '' && nextType == 'ALL') {
                    routing.push(`/main/esearch/weeso${search}`)
                }else if (nextType == 'ERROR') {
                    Modal.error({
                        title: getLabel(15172, "系统提示"),
                        content: nextMsg,
                    });
                } else {
                    const key = nextType || 'ALL';
                    weesoStore.changeKey(key, false);
                    weesoStore.setState(
                        {
                            inputValue: nextVal ? nextVal : query.value,
                            defalutMoreKey: nextType ? nextType : query.type,
                            defaultKey: nextSearchType ? nextSearchType : query.searchType
                        }
                    )
                    weesoStore.resortTopTaps();
                    weesoStore.inputValue !== '' && weesoStore.doSearch();
                }
            }
            weesoStore.isCustomer = false;
        });

  }

  render() {
    const {weesoStore} = this.props;
    let {defaultKey, loading, emptyResult, page, pages, imageResult, defalutMoreKey, inputValue, searchDatas, isSearchSucess, 
      searchKey, tagsDatas, page_resultShowLogo, page_resultLogo
    } = weesoStore;
    const {contentType, normalkey} = searchDatas;
    const { logoVisible } = tagsDatas;
    let numberPage = Number(page);
    emptyResult = toJS(emptyResult);
    const showResult = isSearchSucess ? defalutMoreKey == contentType : true; //判断搜索结果和页面显示的搜索内容是否一致（避免因数据加载速度，前面搜素结果覆盖当下）
    const hideTitle = this.props.location.query.hideTitle;
    let { topTabsVisible, page_descStr } = this.props.weesoSeachSetStore;
    let topHeight = !topTabsVisible&&page_descStr==='' ? 'h-85' : 'h-115';
    if(hideTitle === 'true') {
      if(page_descStr !== '') {
        topHeight = 'h-115';
      } else {
        topHeight = 'h-85';
      }
    }
      const hideSearch = this.props.location.query.hideSearch || '0';
      let topStyle = {};
      if (hideSearch === '1') {
          topStyle = { display: 'none' };
      }
    return (
      <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@a1lyao`} className="esearch-result-container" style={{ height: '100%' }}>
        {page_resultShowLogo && <div className="esearch-result-logo" style={topStyle}>
          <span className={`result-img ${topHeight}`}><img width={77} height={40} src={`${page_resultLogo}`}/></span>
        </div>}
        <div className={page_resultShowLogo ? `wea-weeso wea-weeso-result wea-weeso-logo-visible` : `wea-weeso wea-weeso-result`} style={{height: '100%'}}>
          {/* <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@olwh8w`} spinning={false}> */}
            <div className={`wesult-top-container ${topHeight}`} style={topStyle}>
              <WeesoTop ecId={`${this && this.props && this.props.ecId || ''}_WeesoTop@81y3sc`}/>
              <div className="result-input">
                <WeesoInput ecId={`${this && this.props && this.props.ecId || ''}_WeesoInput@wv0or1`} hideTitle={hideTitle} />
                <SearchTools ecId={`${this && this.props && this.props.ecId || ''}_SearchTools@zpdrbc`}/>
              </div>
              <Adlink ecId={`${this && this.props && this.props.ecId || ''}_Adlink@5fmoeb`}/>
            </div>
          {/* </Spin> */}
          <div className='wea-weeso-spin'>
            <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@oflcf2`} spinning={loading}>
              {
                defaultKey == 'CONTENT' ?
                  <CommonContent ecId={`${this && this.props && this.props.ecId || ''}_CommonContent@piqq00`}  hidesearch={hideSearch}/> :
                  <div className="result-content-images">
                    <WeaNewScrollPagination ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScrollPagination@camkgn`}
                      height={'100%'}
                      onScrollEnd={() => {
                        if (page < pages) {
                          weesoStore.setState({page: `${numberPage + 1}`})
                          weesoStore.inputValue != '' && weesoStore.doSearch();
                        }
                        setTimeout(() => {
                          this.setState({
                            newScrollLoading: false,
                          })
                        }, 2000)
                      }}>
                      {emptyResult ?
                        <div>
                          <div dangerouslySetInnerHTML={{__html: emptyResult}}/>
                        </div> :
                        imageResult && imageResult.lengrh != 0 && imageResult.map((item, index) => {
                          return (
                            <ImagesContent ecId={`${this && this.props && this.props.ecId || ''}_ImagesContent@gdfjwn@${index}`} values={item} index={index}/>
                          )
                        })
                      }
                    </WeaNewScrollPagination>
                  </div>
              }
            </Spin>
          </div>
        </div>
      </Row>
    )
  }
}
